package com.xing.controller;

import com.xing.tools.SingletonClient;
import com.xing.vo.Actor;
import com.xing.vo.Item;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2018/9/7 0007.
 */
@Controller
@RequestMapping(value = "/")
@Configuration
public class LibController {

    @Value("${lib.url}")
    private String libUrl;

    @RequestMapping(value = "/lib")
    public String index(){
        return "redirect:/lib/vl_update";
    }

    @RequestMapping(value = "/lib/{param}")
    public ModelAndView index(ModelAndView modelAndView,
                              @PathVariable(value = "param")String param,
                              @RequestParam(defaultValue = "1",required = false) int page) throws Exception{
        modelAndView.setViewName("lib");
        List<Item> itemList = new ArrayList<>();
        Set<String> idSet = new HashSet<>();
        String url = libUrl+param+".php?page="+page;
        System.out.println(url);
        SingletonClient singletonClient = SingletonClient.getInstance();
        String content = singletonClient.getContent(url);
        Document document = Jsoup.parse(content);
        Elements elements = document.select("div.videos > div.video");
        for(Element element:elements){
            String actorDetail = element.select("a").first().attr("href");
            String detailKey = actorDetail.substring(actorDetail.lastIndexOf("=")+1,actorDetail.length());

            String id = element.select("div.id").first().text();
            String img = element.getElementsByTag("img").first().attr("src");
            img = "http:" + img.replace("ps.jpg","pl.jpg");
            String title = element.select("div.title").first().text();
            Item item = new Item();
            item.setId(id);
            item.setImg(img);
            item.setDetailKey(detailKey);
            item.setTitle(title);
            if(idSet.add(id)) {
                itemList.add(item);
            }
        }

        modelAndView.addObject("page", page);
        modelAndView.addObject("param", param);
        modelAndView.addObject("total",25);
        modelAndView.addObject("itemList", itemList);
        return modelAndView;
    }

    @RequestMapping(value = "/lib/detail4actor/{key}")
    public ModelAndView detail4actor(ModelAndView modelAndView,@PathVariable(required = false,name = "key") String key) throws Exception{
        modelAndView.setViewName("actor-inner");
        String url = libUrl+"?v="+key;
        SingletonClient singletonClient = SingletonClient.getInstance();
        String content = singletonClient.getContent(url);
        Document document = Jsoup.parse(content);
        Elements elements = document.select("span.cast>span.star>a");
        List<Actor> list = new ArrayList<>();
        if(!CollectionUtils.isEmpty(elements)){
            for(Element element:elements) {
                String href = element.attr("href");
                String star = href.substring(href.lastIndexOf("=") + 1, href.length());
                String name = element.text();
                Actor actor = new Actor();
                actor.setName(name);
                actor.setKey(star);
                list.add(actor);
            }
            modelAndView.addObject("actorList", list);
            return modelAndView;
        }
        return modelAndView;
    }

    @RequestMapping(value = "/lib/rank")
    public ModelAndView rank(ModelAndView modelAndView) throws Exception{
        modelAndView.setViewName("rank");
        String url = libUrl+"star_mostfav.php";
        SingletonClient singletonClient = SingletonClient.getInstance();
        String content = singletonClient.getContent(url);
        Document document = Jsoup.parse(content);
        Elements elements = document.select("div.starbox");
        String inner = elements.first().html();
        //特殊逻辑
        inner = inner.replaceAll("vl_star\\.php","star");
        inner = inner.replaceAll("\\?s=","\\\\");
        modelAndView.addObject("inner",inner);
        return modelAndView;
    }

    @RequestMapping(value = "/lib/cate")
    public ModelAndView cate(ModelAndView modelAndView) throws Exception{
        modelAndView.setViewName("cate");
        String url = libUrl+"genres.php";
        SingletonClient singletonClient = SingletonClient.getInstance();
        String content = singletonClient.getContent(url);
        Document document = Jsoup.parse(content);
        Element element = document.getElementById("rightcolumn");
        String inner = element.html();
        //特殊逻辑
        inner = inner.replaceAll("vl_genre\\.php","category");
        inner = inner.replaceAll("\\?g=","\\\\");
        inner = inner.replace("<div class=\"boxtitle\">影片類別</div>","");
        modelAndView.addObject("inner",inner);
        return modelAndView;
    }

    @RequestMapping(value = "/lib/category/{key}")
    public ModelAndView category(ModelAndView modelAndView,@PathVariable(required = false,name = "key") String key,
                                 @RequestParam(defaultValue = "1",required = false) int page) throws Exception{
        modelAndView.setViewName("lib");

        List<Item> itemList = new ArrayList<>();
        Set<String> idSet = new HashSet<>();

        String url = libUrl+"vl_genre.php?g="+key+"&page="+page;
        System.out.println(url);
        SingletonClient singletonClient = SingletonClient.getInstance();
        String content = singletonClient.getContent(url);
        Document document = Jsoup.parse(content);
        Elements elements = document.select("div.videos > div.video");
        for(Element element:elements){
            String actorDetail = element.select("a").first().attr("href");
            String detailKey = actorDetail.substring(actorDetail.lastIndexOf("=")+1,actorDetail.length());

            String id = element.select("div.id").first().text();
            String img = element.getElementsByTag("img").first().attr("src");
            img = "http:" + img.replace("ps.jpg","pl.jpg");
            String title = element.select("div.title").first().text();
            Item item = new Item();
            item.setId(id);
            item.setImg(img);
            item.setDetailKey(detailKey);
            item.setTitle(title);
            if(idSet.add(id)) {
                itemList.add(item);
            }
        }

        int total = 0;
        Element element = document.select("a.page.last").first();
        if(element != null) {
            String tmp = element.attr("href");
            tmp = tmp.substring(tmp.lastIndexOf("=") + 1, tmp.length());
            total = Integer.parseInt(tmp);
        }
        modelAndView.addObject("page", page);
        modelAndView.addObject("param", key);
        modelAndView.addObject("total",total);
        modelAndView.addObject("itemList", itemList);

        return modelAndView;
    }

    @RequestMapping(value = "/lib/star/{key}")
    public ModelAndView start(ModelAndView modelAndView,@PathVariable(required = false,name = "key") String key,
                              @RequestParam(defaultValue = "1",required = false) int page) throws Exception{
        modelAndView.setViewName("lib");
        List<Item> itemList = new ArrayList<>();
        Set<String> idSet = new HashSet<>();

        String url = libUrl+"vl_star.php?s="+key+"&page="+page;
        System.out.println(url);
        SingletonClient singletonClient = SingletonClient.getInstance();
        String content = singletonClient.getContent(url);
        Document document = Jsoup.parse(content);
        Elements elements = document.select("div.videos > div.video");
        for(Element element:elements){
            String id = element.select("div.id").first().text();
            String img = element.getElementsByTag("img").first().attr("src");
            img = "http:" + img.replace("ps.jpg","pl.jpg");
            String title = element.select("div.title").first().text();
            Item item = new Item();
            item.setId(id);
            item.setImg(img);
            item.setTitle(title);
            if(idSet.add(id)) {
                itemList.add(item);
            }
        }

        int total = 0;
        Element element = document.select("a.page.last").first();
        if(element != null) {
            String tmp = element.attr("href");
            tmp = tmp.substring(tmp.lastIndexOf("=") + 1, tmp.length());
            total = Integer.parseInt(tmp);
        }
        modelAndView.addObject("page", page);
        modelAndView.addObject("param", key);
        modelAndView.addObject("total",total);
        modelAndView.addObject("itemList", itemList);
        return modelAndView;
    }
}
